package com.venus.interceptor;


import com.venus.util.JwtToken;
import com.venus.util.Token;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@Component//HandlerInterceptorAdapter
public class LoginInterceptor extends HandlerInterceptorAdapter {


    // 被登录验证拦截器忽略的URl
    private List<String> excludedUrl = new ArrayList<String>();


    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        excludedUrl.clear();
        excludedUrl.add("login");

        // 允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");


        // 获取请求路径，例如：/ent/list
        String requestUri = request.getRequestURI();
        System.out.println("地址：" + requestUri);
        for (String uri : excludedUrl) {
            if (requestUri.contains(uri)) {
                return true;
            }
        }
        String userName = request.getParameter("userName");
        System.out.println("userName：" + userName);

        String rid = request.getParameter("rid");
        System.out.println("rid：" + rid);
        // 获取用户每次请求时，携带的tokenId信息
        String token = request.getParameter("token");
        System.out.println("token：" + token);
        if (token != null && !token.equals("")) {
            return true;
        } else {
            System.out.println("没有token");
            response.sendRedirect("http://127.0.0.1:8020/venus-pre/error1.html");
        }
        return false;
    }

    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }

}
